package project3;

/*
 * This stemmer is based off the Porter Stemming Algorithm found in
 * 
 *     Porter, 1980, An algorithm for suffix stripping, Program, Vol. 14,
 *     no. 3, pp 130-137,
 *
 *     See also http://www.tartarus.org/~martin/PorterStemmer
 * 
 */

public class Stemmer implements project3.WordStemmer{
	private char[] word;
	private int i;
	private int i_end;
	private int j;
	private int k;
	
	//Initialize Stemmer
	public Stemmer()
	   {  word = new char[50];
	      i = 0;
	      i_end = 0;
	   }
	
	//stem the word
	 public String stem(String word){
		try
		{
		   add(word.toCharArray(), word.length());
		   stem();
		   return new String(this.word,0,i_end);
	   }catch(Exception e){System.out.println("Problem stemming "+word);}
		   return word;
	 }
	
	//add chars to the word
	public void add(char[] w, int wLen)
	   {  if (i+wLen >= word.length)
	      {  char[] new_word = new char[i+wLen+50];
	         for (int c = 0; c < i; c++) new_word[c] = word[c];
	         word = new_word;
	      }
	      for (int c = 0; c < wLen; c++) word[i++] = w[c];
	   }
	
	//Is the ith letter of the word a consonant?
	private final boolean cons(int i){
		switch (word[i])
	      {  case 'a': case 'e': case 'i': case 'o': case 'u': return false;
	         case 'y': return (i==0) ? true : !cons(i-1);
	         default: return true;
	      }
	}
	
	//Is the ith letter of the word a vowel?
	private final boolean vowel(int i){
		return !cons(i);
	}

	//Find the number of consonant sequences 
	private final int numConsSequence(){
		  int n = 0;
	      int i = 0;
	      while(true)
	      {  if (i > j) return n;
	         if (vowel(i)) break; i++;
	      }
	      i++;
	      while(true)
	      {  while(true)
	         {  if (i > j) return n;
	               if (cons(i)) break;
	               i++;
	         }
	         i++;
	         n++;
	         while(true)
	         {  if (i > j) return n;
	            if (vowel(i)) break;
	            i++;
	         }
	         i++;
	       }
	}
	
	//Is there a vowel in the stem?
	private final boolean vowelinstem()
	   {  int i; for (i = 0; i <= j; i++) if (vowel(i)) return true;
	      return false;
	   }
	
	//Are the ith and i+1th letters in the word a consonant
	private final boolean doublec(int i)
	   {  if (i < 1) return false;
	      if (word[i] != word[i-1]) return false;
	      return cons(i);
	   }
	
	//Check for a consonant vowel consonant pattern where the last consonant is not a w x or y
	//used to restore e's
	private final boolean cvc(int i)
	   {  if (i < 2 || vowel(i) || cons(i-1) || vowel(i-2)) return false;
	      { 
	         if (word[i] == 'w' || word[i] == 'x' || word[i] == 'y') return false;
	      }
	      return true;
	   }
	
	//Does the word end in string S?
	private final boolean ends(String s)
	   {  
		  int l = s.length();
	      int o = k-l+1;
	      if (o < 0) return false;
	      for (int i = 0; i < l; i++) if (word[o+i] != s.charAt(i)) return false;
	      j = k-l;
	      return true;
	   }
	
	//Set the designated characters in the word to those of string s
	private final void setto(String s)
	   {  int l = s.length();
	      int o = j+1;
	      for (int i = 0; i < l; i++) word[o+i] = s.charAt(i);
	      k = j+l;
	   }
	
	//Sets s if there is more than one consonant sequence
	private final void settoIfConsSequence(String s) { if (numConsSequence() > 0) setto(s); }

	//starts the stemming process
	private final void startStem(){
		//gets rid of plurals, ed, and ing
	    	if (word[k] == 's')
		      {  if (ends("sses")) k -= 2; else
		         if (ends("ies")) setto("i"); else
		         if (word[k-1] != 's') k--;
		      }
		      if (ends("eed")) { if (numConsSequence() > 0) k--; } else
		      if ((ends("ed") || ends("ing")) && vowelinstem())
		      {  k = j;
		         if (ends("at")) setto("ate"); else
		         if (ends("bl")) setto("ble"); else
		         if (ends("iz")) setto("ize"); else
		         if (doublec(k))
		         {  k--;
		            {  int ch = word[k];
		               if (ch == 'l' || ch == 's' || ch == 'z') k++;
		            }
		         }
		         else if (numConsSequence() == 1 && cvc(k)) setto("e");
		     }
		    //turns y to i if there is a vowel in the stem
		      if (ends("y") && vowelinstem()) word[k] = 'i';
		    //if two suffices apply, chose the correct one to continue with
		      switch (word[k-1])
			   {
			       case 'a': if (ends("ational")) { settoIfConsSequence("ate"); break; }
			                 if (ends("tional")) { settoIfConsSequence("tion"); break; }
			                 break;
			       case 'c': if (ends("enci")) { settoIfConsSequence("ence"); break; }
			                 if (ends("anci")) { settoIfConsSequence("ance"); break; }
			                 break;
			       case 'e': if (ends("izer")) { settoIfConsSequence("ize"); break; }
			                 break;
			       case 'l': if (ends("bli")) { settoIfConsSequence("ble"); break; }
			                 if (ends("alli")) { settoIfConsSequence("al"); break; }
			                 if (ends("entli")) { settoIfConsSequence("ent"); break; }
			                 if (ends("eli")) { settoIfConsSequence("e"); break; }
			                 if (ends("ousli")) { settoIfConsSequence("ous"); break; }
			                 break;
			       case 'o': if (ends("ization")) { settoIfConsSequence("ize"); break; }
			                 if (ends("ation")) { settoIfConsSequence("ate"); break; }
			                 if (ends("ator")) { settoIfConsSequence("ate"); break; }
			                 break;
			       case 's': if (ends("alism")) { settoIfConsSequence("al"); break; }
			                 if (ends("iveness")) { settoIfConsSequence("ive"); break; }
			                 if (ends("fulness")) { settoIfConsSequence("ful"); break; }
			                 if (ends("ousness")) { settoIfConsSequence("ous"); break; }
			                 break;
			       case 't': if (ends("aliti")) { settoIfConsSequence("al"); break; }
			                 if (ends("iviti")) { settoIfConsSequence("ive"); break; }
			                 if (ends("biliti")) { settoIfConsSequence("ble"); break; }
			                 break;
			       case 'g': if (ends("logi")) { settoIfConsSequence("log"); break; }
			   }
		    // deals with full ic and ness
		      switch (word[k])
			   {
			       case 'e': if (ends("icate")) { settoIfConsSequence("ic"); break; }
			                 if (ends("ative")) { settoIfConsSequence(""); break; }
			                 if (ends("alize")) { settoIfConsSequence("al"); break; }
			                 break;
			       case 'i': if (ends("iciti")) { settoIfConsSequence("ic"); break; }
			                 break;
			       case 'l': if (ends("ical")) { settoIfConsSequence("ic"); break; }
			                 if (ends("ful")) { settoIfConsSequence(""); break; }
			                 break;
			       case 's': if (ends("ness")) { settoIfConsSequence(""); break; }
			                 break;
			   }
	   }
	   
	 //takes of the end of the stem in certain situations
	   private final void endStem(){
		   if (k == 0) return; /* for Bug 1 */ switch (word[k-1])
	       {  case 'a': if (ends("al")) break; return;
	          case 'c': if (ends("ance")) break;
	                    if (ends("ence")) break; return;
	          case 'e': if (ends("er")) break; return;
	          case 'i': if (ends("ic")) break; return;
	          case 'l': if (ends("able")) break;
	                    if (ends("ible")) break; return;
	          case 'n': if (ends("ant")) break;
	                    if (ends("ement")) break;
	                    if (ends("ment")) break;
	                    /* element etc. not stripped before the m */
	                    if (ends("ent")) break; return;
	          case 'o': if (ends("ion") && j >= 0 && (word[j] == 's' || word[j] == 't')) break;
	                                    /* j >= 0 fixes Bug 2 */
	                    if (ends("ou")) break; return;
	                    /* takes care of -ous */
	          case 's': if (ends("ism")) break; return;
	          case 't': if (ends("ate")) break;
	                    if (ends("iti")) break; return;
	          case 'u': if (ends("ous")) break; return;
	          case 'v': if (ends("ive")) break; return;
	          case 'z': if (ends("ize")) break; return;
	          default: return;
	       }
	       if (numConsSequence() > 1) k = j;
	   }
	   
	   //sets k so that we know where the stemmed word ends
	   private final void finalizeStem(){
		   j = k;
		   if (word[k] == 'e')
		   {  int a = numConsSequence();
		      if (a > 1 || a == 1 && !cvc(k-1)) k--;
		   }
		   if (word[k] == 'l' && doublec(k) && numConsSequence() > 1) k--;
	   }
	   
	   //Stem the word
	   public void stem()
	   {  k = i - 1;
	      if (k > 1) {
	    	  startStem();
	    	  endStem();
	    	  finalizeStem();
	      }
	      i_end = k+1; i = 0;
	   }
	
}

